﻿using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PropertyManagementSystemAPI.Models;
using System.Xml.Linq;
using static System.Net.Mime.MediaTypeNames;

namespace PropertyManagementSystemAPI.Controllers.Complaints
{
    /// <summary>
    /// 投诉
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
 /*   [EnableCors("any")]*/
    [ApiExplorerSettings(GroupName = "物业")]
    public class ComplaintsController : ControllerBase
    {
        public readonly IFreeSql db;
        public ComplaintsController(IFreeSql db)
        {
            this.db = db;
        }
        //[HttpGet]
        //public object getserch()
        //{
        //   var list= db.Select<Models.Complaints>().ToList();
        //   return list;
        //}
        [HttpGet]
        public object sel(int cCellId)
        {
            var list = db.Select<Models.Complaints, Models.CellInformation>().
                LeftJoin((a, b) => a.CCellId == b.CellId).ToList((a, b) => new { a.CCellId,a.CUserId,a.CId
                ,a.CText,a.CCreatTime,a.CTitle,a.CApplication,a.CIsMagic, b.CellId,b.CellName });
            return list;
        }
        [HttpPost]
        public object Add([FromBody]Models.Complaints complaints)
        {
            var item = new Models.Complaints
            {
                CCellId=complaints.CCellId,
                CId=complaints.CId,
                CUserId=complaints.CUserId,
                CIsMagic = complaints.CIsMagic,
                CText = complaints.CText,
                CTitle = complaints.CTitle,
                CCreatTime=DateTime.Now,
                CApplication=complaints.CApplication,
            };

            var list= db.Insert(item).ExecuteAffrows();
           return list;
        }
        [HttpGet("sel")]
        public object select()
        {
            var sel = db.Select<CellInformation>().ToList
                (c => new { c.CellId, c.CellName });
            return sel;
        }
        [HttpGet("GCellId")]
        public object GCellId(int cCellId)
        {
            var list = db.Select<Models.Complaints, CellInformation>()
                .LeftJoin((a, b) => a.CCellId == b.CellId)
                .Where((a, b) => a.CCellId == cCellId)
                .ToList((a, b) => new {
                    a.CCellId,
                    a.CUserId,
                    a.CId,
                    a.CText,
                    a.CCreatTime,
                    a.CTitle,
                    a.CApplication,
                    a.CIsMagic, b.CellName, b.CellId });
            int totalCount = list.Count();//总条数
            return new
            {
                TotalCount = totalCount,
                Data = list
            };
        }
    }
}
